package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 *  孕婴童商务网   网站解析
 * @author Chase	
 *
 */
public class Yyt360NewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		"http://www.yyt360.com/[a-z]*/[0-9]*.shtml",
//		"http://www.yyt360.com/skin/yyt360/css/yyt360_index.css",
//		"http://www.yyt360.com/[a-z]*/",
//		"http://www.yyt360.com/e/tags/[?]tagname=[A-Z0-9\\-%]+"
	};
		
	
			
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String url=urlMeta.getUrl();	
		/*
		 * 网站源码
		 */
		String htmltxt=urlMeta.getHtml();
			
		if(!isDetailPage(url)){
		}				
		
		
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		
		/*
		 * 标题
		 */
		title=doc.select("h1.content_title").text();
		title=StringUtils.substringBefore(title, "_");
		
		if(StringUtils.isBlank(title)){
		}
		
		/*
		 * 内容
		 */
		content=HtmlCleaner.getContentHtml(url,doc.select("div.content_view_content_body.clearfix"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.article-content"));
		}
		
		
		/*
		 * 作者
		 */
		String authorStr=null;
		//authorStr=doc.select("div.article-more").text();
		author=StringUtils.substringBetween(authorStr, "来源：");
		if(StringUtils.isBlank(author)){
			author=StringUtils.substringAfter(authorStr, "来源：");
		}
	
		/*
		 * 时间
		 */
		date=DateUtils.matchDate(doc.select("span.time").text());
		
		/*
		 * 链接
		 */
		news.setUrl(url);	
		
		/*
		 * 来源
		 */
		news.setTitle(title);
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);
		return news;
	}
	
	public static void main(String[] args) {	
		Yyt360NewsAnalyse analyse=new Yyt360NewsAnalyse();
		String url="http://www.yyt360.com/article/2015091525890.shtml";
		url=UrlArgumentTop.FromatUrl(url);
		System.err.println(url);
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	

	
	public boolean isNeedUpdate(){
		return false;
	}
}
